package com.nota.flink.cdc.pojo;

import cn.hutool.core.date.DateTime;
import com.nota.flink.cdc.pojo.enums.ResultCode;
import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;


@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    public int c;


    public String m = "";


    public T d;


    public String t = DateTime.now().toString();

    public Result() {

    }

    public Result(int c, String m, T d) {
        this.c = c;
        this.m = m;
        this.d = d;
    }

    public static <T> Result<T> ok(T d) {
        Result<T> result = new Result<>();
        result.setC(ResultCode.OK.getCode());
        result.setD(d);
        result.setM(ResultCode.OK.getMsg());
        return result;
    }

    public static <T> Result<T> okMsg(String msg) {
        Result<T> result = new Result<>();
        result.setC(ResultCode.OK.getCode());
        result.setM(ResultCode.OK.getMsg());
        return result;
    }

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setC(ResultCode.OK.getCode());
        result.setM(ResultCode.OK.getMsg());
        return result;
    }

    public static <T> Result<T> fail(T d, String msg) {
        Result<T> result = new Result<>();
        result.setC(ResultCode.FAIL.getCode());
        result.setD(d);
        result.setM(ObjectUtils.isEmpty(msg) ? ResultCode.FAIL.getMsg() : msg);
        return result;
    }

    public static <T> Result<T> failMsg(String msg) {
        Result<T> result = new Result<>();
        result.setC(ResultCode.FAIL.getCode());
        result.setM(ObjectUtils.isEmpty(msg) ? ResultCode.FAIL.getMsg() : msg);
        return result;
    }

    public static <T> Result<T> build(ResultCode code, T d, String msg) {
        Result<T> result = new Result<>();
        result.setC(code.getCode());
        result.setD(d);
        result.setM(msg);
        return result;
    }

    public static <T> Result<?> unauthorized() {
        Result<?> result = new Result<>();
        result.setC(ResultCode.unauthorized.getCode());
        result.setM(ResultCode.unauthorized.getMsg());
        return result;
    }
}
